home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / applet / communication / example / GetApplets.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.1 KB  |  65 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.*;
  18. import java.awt.*;
  19. import java.util.Enumeration;
  20.  
  21. public class GetApplets extends Applet {
  22.     private TextArea textArea;
  23.  
  24.     public void init() {
  25.         setLayout(new BorderLayout());
  26.  
  27.         add("North", new Button("Click to call getApplets()"));
  28.  
  29.         textArea = new TextArea(5, 40);
  30.         textArea.setEditable(false);
  31.         add("Center", textArea);
  32.  
  33.         validate();
  34.     }
  35.  
  36.     public boolean action(Event event, Object o) {
  37.         printApplets();
  38.         return false;
  39.     }
  40.  
  41.     public String getAppletInfo() {
  42.         return "GetApplets by Kathy Walrath";
  43.     }
  44.  
  45.     public void printApplets() {
  46.         //Enumeration will contain all applets on this page (including
  47.         //this one) that we can send messages to.
  48.         Enumeration e = getAppletContext().getApplets();
  49.  
  50.         textArea.appendText("Results of getApplets():\n");
  51.  
  52.         while (e.hasMoreElements()) {
  53.             Applet applet = (Applet)e.nextElement();
  54.             String info = ((Applet)applet).getAppletInfo();
  55.             if (info != null) {
  56.                 textArea.appendText("- " + info + "\n");
  57.             } else {
  58.                 textArea.appendText("- " + applet.getClass().getName() + "\n");
  59.             } 
  60.         }
  61.         textArea.appendText("________________________\n\n");
  62.     }
  63. }
  64.  
  65.